package com.yangkunhong.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yangkunhong.constant.ErrMsg;
import com.yangkunhong.dao.UserLogDao;
import com.yangkunhong.pojo.PageDataResponse;
import com.yangkunhong.pojo.User;
import com.yangkunhong.pojo.UserLog;
import com.yangkunhong.requestVo.UserLogVo;
import com.yangkunhong.service.UserLogService;
import com.yangkunhong.service.UserService;
import com.yangkunhong.utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = {
        "/UserLogServlet136/list","/UserLogServlet136/detail","/UserLogServlet136/getPageData",
        "/UserLogServlet136/deleteById","/UserLogServlet136/update","/UserLogServlet136/showInfo"
})
public class UserLogServlet136 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("控制器收到GET请求");

        //能够识别list、detail /GoodsServlet136/list /GoodsServlet136/detail
        //解析请求参数
        if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/list")){
            req.setAttribute("active","user_log_list");
            //获取请求参数

            //日志列别

            req.getRequestDispatcher("/frontend/user_center.jsp").forward(req,resp);
        }else  if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/detail")){

            UserLogService userLogService = new UserLogService();
            req.setAttribute("dataInfo",userLogService.getOneById(Integer.parseInt(req.getParameter("id"))));
            req.getRequestDispatcher("/frontend/user_log_detail.jsp").forward(req,resp);

        }else  if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/showInfo")){

            UserLogService userLogService = new UserLogService();
            req.setAttribute("dataInfo",userLogService.getOneById(Integer.parseInt(req.getParameter("id"))));
            req.getRequestDispatcher("/frontend/user_log_info.jsp").forward(req,resp);

        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/getPageData")){

                //获取分页数据
                Integer page = Integer.valueOf(req.getParameter("page"));
                Integer limit = Integer.valueOf(req.getParameter("limit"));
                UserLogVo userLogVo = new UserLogVo();
                userLogVo.setUsername(req.getParameter("username"));
                userLogVo.setCreateAt(req.getParameter("createAt"));
                userLogVo.setCreateIp(req.getParameter("createIp"));
                userLogVo.setOp(req.getParameter("op"));
                userLogVo.setPage(page);
                userLogVo.setLimit(limit);
                PageDataResponse<List<UserLog>> listPageDataResponse = UserLogDao.findUserLogListByUsername(userLogVo);
                JSONUtils.sendJsonStr(listPageDataResponse,resp);

        }
        else {
            req.getRequestDispatcher("/frontend/user_log_detail.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1 接收参数
        String json = JSONUtils.parseJsonInputStream(req);
        //2 解析数据
        System.out.println(json);
        Gson gson = new Gson();
        Type mapType = new TypeToken<Map<String, String>>() {}.getType();
        Map map = gson.fromJson(json, mapType);
        UserLogService userLogService = new UserLogService();
        ErrMsg errMsg = ErrMsg.OK;
        User userInfo = (User) req.getSession().getAttribute("USER_INFO");
        if(userInfo == null){
            JSONUtils.sendJsonStr( ErrMsg.NO_LOGIN,resp);
        }
        if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/update")){
            errMsg = userLogService.doUpdate(map);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserLogServlet136/deleteById")){
             errMsg = userLogService.deleteById(map);
        }
        //5 发送数据到浏览器【做出响应】
        JSONUtils.sendJsonStr(errMsg,resp);
    }
}
